#!/bin/bash

# 项目停止脚本

echo "正在停止 Architecture Drawing Demo..."

# 读取进程ID
if [ -f .backend.pid ]; then
    BACKEND_PID=$(cat .backend.pid)
    if kill -0 $BACKEND_PID 2>/dev/null; then
        echo "停止后端服务 (PID: $BACKEND_PID)"
        kill $BACKEND_PID
        sleep 2
        if kill -0 $BACKEND_PID 2>/dev/null; then
            echo "强制停止后端服务"
            kill -9 $BACKEND_PID
        fi
    fi
    rm -f .backend.pid
fi

if [ -f .frontend.pid ]; then
    FRONTEND_PID=$(cat .frontend.pid)
    if kill -0 $FRONTEND_PID 2>/dev/null; then
        echo "停止前端服务 (PID: $FRONTEND_PID)"
        kill $FRONTEND_PID
        sleep 2
        if kill -0 $FRONTEND_PID 2>/dev/null; then
            echo "强制停止前端服务"
            kill -9 $FRONTEND_PID
        fi
    fi
    rm -f .frontend.pid
fi

# 清理可能的遗留进程
pkill -f "mvn spring-boot:run"
pkill -f "vite"

echo "✅ 所有服务已停止"